<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>01过滤器</title>
  <script src="../js/vue.js"></script>
  <script src="../js/day.js"></script>
</head>
<body>
  <div id="app">
    <h1>{{title}}</h1>
    <h3>{{time}}</h3>
    <h3>{{formatTimeByMethods()}}</h3>
    <h3>{{formatTimeByComputed}}</h3>
    <h3>{{time | formatTimeByFilter}}</h3>
    <h3>{{time | filterParams('YYYY-MM-DD')}}</h3>
    <h3>{{time | formatTimeByFilter | sliceTime}}</h3>
  </div>
  <script>
    /* 
      过滤器
        语法:
          1 Vue.filter('xxx', callback), new Vue({ filters: { xxx(val) {} } })
          2 {{xxx | 过滤器}} 或 v-bind:xxx="xxx | 过滤器"
        注意:
          使用过滤器时可以传参，也可以串联使用过滤器
          过滤器只是将原数据加工处理后生成另一个新数据，不会改变原数据
    */
    Vue.filter('sliceTime', function(val) {
      return val.slice(0, 4)
    })
    new Vue({
      el: '#app',
      data() {
        return {
          title: '过滤器',
          time: 1710247766962
        }
      },
      computed: {
        formatTimeByComputed() {
          return dayjs(this.time).format('YYYY-MM-DD HH:mm:ss')
        }
      },
      methods: {
        formatTimeByMethods() {
          return dayjs(this.time).format('YYYY-MM-DD HH:mm:ss')
        }
      },
      filters: {
        formatTimeByFilter(val) {
          return dayjs(val).format('YYYY-MM-DD HH:mm:ss')
        },
        filterParams(val, str) {
          return dayjs(val).format(str)
        }
        /* sliceTime(val) {
          return val.slice(0, 4)
        } */
      }
    })
  </script>
</body>
</html>